package qky.api.controller.user;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qky.api.entity.FirmEntity;
import qky.api.entity.UserEntity;
import qky.api.service.FirmService;
import qky.api.service.UserService;
import qky.api.service.VerifyCodeService;
import qky.common.utils.MapParamBuilder;
import qky.common.utils.R;

import java.util.List;


@RestController
@RequestMapping("/api/user")
@Api(tags = "用户信息", description="【OK】")
public class ApiUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private VerifyCodeService verifyCodeService;

    @Autowired
    private FirmService firmService;

    @PostMapping("update/pd")
    @ApiOperation(value = "修改密码", notes = "mobile:string,newPassword:string")
    public R setNewPassword(@RequestBody JSONObject paramJson) {
        UserEntity userEntity = userService.getByMobile(paramJson.getString("mobile"));
        if (userEntity == null) {
            return R.error("此手机号尚未注册");
        }
        userEntity.setPassword(DigestUtils.sha256Hex(paramJson.getString("newPassword")));
        userService.update(userEntity);
        return R.ok();
    }

    @PostMapping("update/mobile")
    @ApiOperation(value = "修改手机号", notes = "mobile:string,code:string")
    public R updatePhone(@RequestBody JSONObject paramJson) {
        String mobile = paramJson.getString("mobile");
        if (!verifyCodeService.checkVerifyCode(mobile, paramJson.getString("code"))) {
            return R.error("验证码错误或已失效");
        }
        UserEntity userEntity = userService.getByMobile(mobile);
        if (userEntity != null) {
            return R.error("此手机号已注册");
        }

        userEntity.setMobile(mobile);
        userService.update(userEntity);
        return R.ok();
    }

    @PostMapping("update/info")
    @ApiOperation(value = "个人信息修改", notes = "userId:string,nickName:string,gender:男1女2")
    public R updateUser(@RequestBody JSONObject paramJson) {
        UserEntity user = new UserEntity();
        user.setUserId(paramJson.getString("userId"));
        user.setNickName(paramJson.getString("nickName"));
        user.setGender(paramJson.getInteger("gender"));

        userService.update(user);
        return R.ok();
    }

    @PostMapping("info")
    @ApiOperation(value = "个人信息详情", notes = "userId:string")
    public R userInfo(@RequestBody JSONObject paramJson) {
        UserEntity entity = userService.query(paramJson.getString("userId"));
        List<FirmEntity> firmList = firmService.queryList(new MapParamBuilder<String, Object>().param("EQ_registerUserId", entity.getUserId()).get());
        if (!CollectionUtils.isEmpty(firmList)) {
            entity.setFirmList(firmList);
        }
        return R.ok().putData(entity);
    }

}
